package de.szut.Pong.GUI;

import java.io.File;

import javax.swing.JFileChooser;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;


public class Menu extends JMenuBar {

	private static final long serialVersionUID = 6986686811317721045L;
	private Model model;
	private JMenu mnGame;
	private JMenu mnOptions;
	private JMenuItem mntmNewGame;
	private JMenuItem mntmLeftAiselection;
	private JMenuItem mntmRightAiselection;
	private AILoader aiLoader;
	
	public Menu(Model model) {
		this.model = model;
		aiLoader = new AILoader();
		mnGame = new JMenu("Game");
		add(mnGame);
		mnOptions = new JMenu("Options");
		add(mnOptions);
		mntmNewGame = new JMenuItem("New Game");
		mnGame.add(mntmNewGame);
		mntmLeftAiselection = new JMenuItem("Choose Left AI");
		mnOptions.add(mntmLeftAiselection);
		mntmRightAiselection = new JMenuItem("Choose Right AI");
		mnOptions.add(mntmRightAiselection);
		
		mntmNewGame.addActionListener(e -> {
			model.start();
		});
		
		mntmLeftAiselection.addActionListener(e -> {
			JFileChooser fileChooser = new JFileChooser();
            int option = fileChooser.showOpenDialog(null);
            if (option == JFileChooser.APPROVE_OPTION) {
            	model.setLeftUser(aiLoader.loadClassObject(fileChooser.getSelectedFile()));
            }
		});
		
		mntmRightAiselection.addActionListener(e -> {
			JFileChooser fileChooser = new JFileChooser();
            int option = fileChooser.showOpenDialog(null);
            if (option == JFileChooser.APPROVE_OPTION) {
            	model.setRightUser(aiLoader.loadClassObject(fileChooser.getSelectedFile()));
            }
		});
	}
}
